package com.jinbai.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jinbai.util.DBUtil;
import com.jinbai.util.Log;
import com.jinbai.view_object.UserVO;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/studio/album/add.json")
public class addTracksServlet extends HttpServlet {
     public class TrackView {
        public Integer tid;
        public String title;

        public TrackView(int tid, String title) {
            this.tid = tid;
            this.title = title;
        }
    }

    public class Result {
         public List<TrackView> trackViewList;
         public String aid;

         public Result(List<TrackView> trackViewList, String aid) {
             this.trackViewList = trackViewList;
             this.aid = aid;
         }
    }

    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserVO userVO = null;
        final HttpSession session = req.getSession(false);
        if (session != null) {
            userVO = (UserVO) session.getAttribute("currentUser");
        }
        if (userVO == null) {
            Log.println("用户未登录");
            resp.sendRedirect("studio/user/login.html");
            return;
        }
        final String aid = req.getParameter("aid");
        List<String> published = new ArrayList<>();
        List<TrackView> ansList = new ArrayList<>();
        try (Connection con = DBUtil.connection()) {
            {
                String sql = "select tid from relations where aid = ?";
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                    ps.setString(1, aid);
                    Log.println("执行SQL: " + ps);
                    try (final ResultSet rs = ps.executeQuery()) {
                        while (rs.next()) {
                            published.add(rs.getString("tid"));
                        }
                    }
                }
            }
            if (!published.isEmpty()) {
                final String s = String.join(", ", published);
                String pre = "select tid, title from tracks where tid not in (%s) and uid = ? order by tid desc";
                final String sql = String.format(pre, s);
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                    ps.setInt(1, userVO.getUid());
                    Log.println("执行SQL: " + ps);
                    try (final ResultSet rs = ps.executeQuery()) {
                        while (rs.next()) {
                            ansList.add(new TrackView(rs.getInt("tid"), rs.getString("title")));
                        }
                    }
                }
            } else {
                throw new RuntimeException("该专辑没有对应的歌曲");
            }
        }
        Result result = new Result(ansList, aid);
        ObjectMapper om = new ObjectMapper();
        final String json = om.writeValueAsString(result);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().println(json);
    }
}
